﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.Core;
    using Hidistro.Membership.Context;
    using Hidistro.Membership.Core;
    using System;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;

    public class Logout : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            //this.Page.Response.Redirect("http://account.x-juse.com/Logout.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority) + "&ReturnUrl=" + HttpUtility.UrlEncode(this.Page.Request.QueryString["ReturnUrl"]));
            if (NginxServer.IsInit)
            {
            }
            else
                NginxServer.Init();

            string strNgserverName = NginxServer.GetNginxServerName();
            string strAccountserverName = NginxServer.GetAccountServerName();
            string strredir;

            if (string.IsNullOrEmpty(strNgserverName))
            {
                if (string.IsNullOrEmpty(strAccountserverName))
                    strredir = "http://account.x-juse.com/Logout.aspx?sdomain=" +System.Web.HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority) + "&ReturnUrl=" + HttpUtility.UrlEncode(this.Page.Request.QueryString["ReturnUrl"]);
                else
                    strredir = "http://" + strAccountserverName + "/Logout.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority) + "&ReturnUrl=" + HttpUtility.UrlEncode(this.Page.Request.QueryString["ReturnUrl"]);
            }
            else
            {
                if (string.IsNullOrEmpty(strAccountserverName))
                    strredir = "http://account.x-juse.com/Logout.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + strNgserverName) + "&ReturnUrl=" + HttpUtility.UrlEncode(this.Page.Request.QueryString["ReturnUrl"]);
                else
                    strredir = "http://" + strAccountserverName + "/Logout.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + strNgserverName) + "&ReturnUrl=" + HttpUtility.UrlEncode(this.Page.Request.QueryString["ReturnUrl"]);

            }

            this.Page.Response.Redirect(strredir);
            base.OnLoad(e);
            HttpCookie cookie = HiContext.Current.Context.Request.Cookies["Token_" + HiContext.Current.User.UserId.ToString()];
            if (!((cookie == null) || string.IsNullOrEmpty(cookie.Value)))
            {
                cookie.Expires = DateTime.Now;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            HttpCookie cookie2 = HiContext.Current.Context.Request.Cookies["Vshop-Member"];
            if (!((cookie2 == null) || string.IsNullOrEmpty(cookie2.Value)))
            {
                cookie2.Expires = DateTime.Now;
                HttpContext.Current.Response.Cookies.Add(cookie2);
            }
            if (this.Context.Request.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(HiContext.Current.User.Username, true);
                IUserCookie userCookie = HiContext.Current.User.GetUserCookie();
                if (userCookie != null)
                {
                    userCookie.DeleteCookie(authCookie);
                }
                RoleHelper.SignOut(HiContext.Current.User.Username);
                this.Context.Response.Cookies["hishopLoginStatus"].Value = "";
            }
            this.Context.Response.Redirect(Globals.GetSiteUrls().Home, true);
        }
    }
}

